package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate redisTemplate;

//    根据评论id点赞
    @RequestMapping(value = "thumbup/{commentId}",method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String commentId){
        //把用户点赞信息保存在redis中
        //每次点赞之前，先查询用户点赞信息
        //如果没有点赞信息，用户可以点赞
        //如果有点赞信息，用户不能重复点赞

        //模拟用户id
        String userId = "123";
        //查询用户点赞信息，根据用户id和评论id
        Object flag = redisTemplate.opsForValue().get("thumbup_" + userId + "_" + commentId);
        //判断查询到的结果是否为空
        if (flag == null){
            //    如果为空，表示用户没有点过赞，可以点赞
            commentService.thumbup(commentId);
        //    点赞成功，保存点赞信息
            redisTemplate.opsForValue().set("thumbup_" + userId + "_" + commentId,1);
            return new Result(true,StatusCode.OK,"点赞成功");
        }
        //    如果不为空，表示用户点过赞，不可以重复点赞
        return new Result(false,StatusCode.REPERROR,"不能重复点赞");
    }

//    根据id查询文章评论
    @RequestMapping(value = "article/{article}",method = RequestMethod.GET)
    public Result findByArticleId(@PathVariable String article){
        List<Comment> list = commentService.findByArticleId(article);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

//    查询所有评论
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Comment> list = commentService.findAll();

        return new Result(true, StatusCode.OK,"查询成功",list);
    }
    //    根据评论id查询评论数据
    @RequestMapping(value = "{commentId}",method = RequestMethod.GET)
    public Result findById(@PathVariable String commentId){
        Comment comment = commentService.findById(commentId);

        return new Result(true,StatusCode.OK,"查询成功",comment);
    }

//    新增评论
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Comment comment){
        commentService.save(comment);
        return new Result(true,StatusCode.OK,"新增成功",comment);
    }

//    修改评论
    @RequestMapping(value = "{commentId}" ,method = RequestMethod.PUT)
    public Result updateId(@PathVariable String commentId,@RequestBody Comment comment){
    //   设置评论主键
        comment.set_id(commentId);
    //    执行修改
        commentService.updateById(comment);
        return new Result(true,StatusCode.OK,"修改成功");
    }

//    删除评论
    @RequestMapping(value = "{commentId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String commentId){
        commentService.deleteById(commentId);
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
